import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class StartMenu extends JPanel{

	private static final long serialVersionUID = 1L;
	InterfaceFrame frame;
	CustomButton newGame, load, exit, klar;
	CustomPetButton pet1,pet2, pet3, pet4;
	JButton diff1, diff2, diff3, diff4;
	ImageIcon imgPet1 = new ImageIcon(getClass().getResource("/petsPictures/bearStarter.gif"));
	ImageIcon imgPet2 = new ImageIcon(getClass().getResource("/petsPictures/baconatorStarter.gif"));
	ImageIcon imgPet3 = new ImageIcon(getClass().getResource("/petsPictures/seppoStarter.gif"));
	ImageIcon imgPet4 = new ImageIcon(getClass().getResource("/petsPictures/evilBunnyStarter.gif"));
	ImageIcon icon = new ImageIcon(getClass().getResource("/Pics/KOtransp.gif"));
	Font font = new Font("Verdana", Font.PLAIN, 25);
	Font choiceFont = new Font("Verdana", Font.PLAIN, 30);
	Font font2 = new Font("Verdana", Font.PLAIN, 24);
	JLabel iconLabel = new JLabel("",icon,JLabel.CENTER);
	JLabel label= new JLabel("Spelarens Namn:");
	JLabel choice = new JLabel("Välj husdjur", JLabel.CENTER);
	TextField nameField= new TextField(40);
	JLabel name;
	Difficulty chosenDiff;
	GridBagConstraints c= new GridBagConstraints();
	Pet chosen;
	public StartMenu(InterfaceFrame frame){
		
		choice.setFont(choiceFont);
		choice.setForeground(Color.WHITE);
		choice.setMaximumSize(new Dimension(200,50));
		choice.setMinimumSize(new Dimension(200,50));
		choice.setPreferredSize(new Dimension(200,50));
		
		this.frame=frame;
		setBackground(Color.black); 
		setLayout(new GridBagLayout());
		//640 x 400
		pet1= new CustomPetButton(imgPet1);
		pet2= new CustomPetButton(imgPet2);
		pet3= new CustomPetButton(imgPet3);
		pet4= new CustomPetButton(imgPet4);
		
		pet1.setContentAreaFilled(false);
		pet2.setContentAreaFilled(false);
		pet3.setContentAreaFilled(false);
		pet4.setContentAreaFilled(false);
		
		pet1.setBorderPainted(false);
		pet2.setBorderPainted(false);
		pet3.setBorderPainted(false);
		pet4.setBorderPainted(false);
		
		diff1=new CustomButton("Enkel");
		diff2=new CustomButton("Normal");
		diff3=new CustomButton("Svår");
		diff4=new CustomButton("Hårdkärna");
		
		newGame= new CustomButton("Nytt spel");
		load= new CustomButton("Ladda spel");
		klar=new CustomButton("Klar");
		exit= new CustomButton("Avsluta");
		
		
		diff1.addActionListener(new ButtonListener());
		diff2.addActionListener(new ButtonListener());
		diff3.addActionListener(new ButtonListener());
		diff4.addActionListener(new ButtonListener());
		
		pet1.addActionListener(new ButtonListener());
		pet2.addActionListener(new ButtonListener());
		pet3.addActionListener(new ButtonListener());
		pet4.addActionListener(new ButtonListener());

		newGame.addActionListener(new ButtonListener());
		klar.addActionListener(new ButtonListener());
		load.addActionListener(new ButtonListener());
		exit.addActionListener(new ButtonListener());
		
		c.gridx=2;
		c.gridy=2;
		add(iconLabel,c);
		c.gridx=2;
		c.gridy=3;
		add(newGame,c);
		if(new File("newsave.xml").isFile()){
		c.gridx=2;
		c.gridy=4;
		add(load,c);
		}
		c.gridx=2;
		c.gridy=5;
		add(exit,c);
		revalidate();
		
	}
	
	
	private class CustomButton extends JButton{
		
		private static final long serialVersionUID = 1L;
		
		CustomButton(String text){
			this.setText(text);
			this.setContentAreaFilled(false);
			this.setBorderPainted(false);
			this.setForeground(Color.WHITE);
			this.setFont(font);
			this.addMouseListener(new BorderListener());
		}
		
		@Override
		public Dimension getMinimumSize(){
			return new Dimension(200,50);	
		}
		@Override
		public Dimension getMaximumSize(){
			return new Dimension(200,50);	
		}
		@Override
		public Dimension getPreferredSize(){
			return new Dimension(200,50);	
		}
			
	}
	
	private class CustomPetButton extends JButton{
		
		private static final long serialVersionUID = 1L;
		
		CustomPetButton(ImageIcon imgPet1){
			this.setIcon(imgPet1);
			this.setContentAreaFilled(false);
			this.setBorderPainted(false);
			this.setForeground(Color.WHITE);
			this.setFont(font2);
			this.addMouseListener(new BorderListener());
		}
		
		@Override
		public Dimension getMinimumSize(){
			return new Dimension(256,256);	
		}
		@Override
		public Dimension getMaximumSize(){
			return new Dimension(256,256);	
		}
		@Override
		public Dimension getPreferredSize(){
			return new Dimension(256,256);	
		}
			
	}
	
	private class ButtonListener implements ActionListener{

		
	@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(newGame)){
				
				//nameField.setBackground(Color.white);
				newGame.setVisible(false);
				remove(newGame);
				remove(iconLabel);
				/*label.setForeground(Color.white);
				c.gridx=1;
				c.gridy=1;
				c.gridwidth=2;
				add(label,c);
				c.gridx=1;
				c.gridy=2;
				c.gridwidth=2;
				add(nameField,c);*/
				
				c.gridx=1;
				c.gridy=1;
				c.gridwidth=1;
				c.fill = GridBagConstraints.HORIZONTAL;
				add(pet1,c);
				c.gridx=2;
				c.gridy=1;
				c.fill = GridBagConstraints.HORIZONTAL;
				add(pet2,c);
				c.gridx=3;
				c.gridy=1;
				c.fill = GridBagConstraints.HORIZONTAL;
				add(pet3,c);
				c.gridx=4;
				c.gridy=1;
				c.fill = GridBagConstraints.HORIZONTAL;
				add(pet4,c);
				
				c.gridx=2;
				c.gridy=0;
				c.gridwidth=2;
				add(choice,c);
				c.gridx=2;
				c.gridy=4;
				c.gridwidth=2;
				add(load,c);
				c.gridx=2;
				c.gridy=5;
				add(exit,c);
				revalidate();
				repaint();
			
			}else if(e.getSource().equals(pet1)){
				chosen=new Bear("Björn",0);
				/*c.gridx=1;
				c.gridy=3;
				c.gridwidth=2;
				add(klar,c);*/
				remove(choice);
				remove(pet1);
				remove(pet2);
				remove(pet3);
				remove(pet4);
				nameField.setBackground(Color.white);
				label.setForeground(Color.white);
				iconLabel.setIcon(imgPet1);
				c = new GridBagConstraints();
				c.gridx=2;
				c.gridy=0;
				add(iconLabel,c);
				c.gridx=2;
				c.gridy=1;
				add(label,c);
				c.gridx=2;
				c.gridy=2;
				add(nameField,c);
				c.gridx=2;
				c.gridy=3;
				add(klar,c);
				c.gridx=2;
				c.gridy=4;
				add(load,c);
				c.gridx=2;
				c.gridy=5;
				add(exit,c);
				//c.gridx=0;
				//c.gridy=0;
				//c.gridwidth=1;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff1,c);
				//c.gridx=1;
				//c.gridy=0;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff2,c);
				//c.gridx=2;
				//c.gridy=0;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff3,c);
				//c.gridx=3;
				//c.gridy=0;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff4,c);
				revalidate();
				repaint();
				
			}else if(e.getSource().equals(pet2)){
				chosen=new Baconator("Baconator",0);
				remove(choice);
				remove(pet1);
				remove(pet2);
				remove(pet3);
				remove(pet4);
				nameField.setBackground(Color.white);
				label.setForeground(Color.white);
				iconLabel.setIcon(imgPet2);
				c = new GridBagConstraints();
				c.gridx=2;
				c.gridy=0;
				add(iconLabel,c);
				c.gridx=2;
				c.gridy=1;
				add(label,c);
				c.gridx=2;
				c.gridy=2;
				add(nameField,c);
				c.gridx=2;
				c.gridy=3;
				add(klar,c);
				c.gridx=2;
				c.gridy=4;
				add(load,c);
				c.gridx=2;
				c.gridy=5;
				add(exit,c);
				//c.gridx=0;
				//c.gridy=0;
				//c.gridwidth=1;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff1,c);
				//c.gridx=1;
				//c.gridy=0;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff2,c);
				//c.gridx=2;
				//c.gridy=0;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff3,c);
				//c.gridx=3;
				//c.gridy=0;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff4,c);
				revalidate();
				repaint();
				
			
			}else if(e.getSource().equals(pet3)){
				chosen=new Seppo("Seppo",0);
				remove(choice);
				remove(pet1);
				remove(pet2);
				remove(pet3);
				remove(pet4);
				nameField.setBackground(Color.white);
				label.setForeground(Color.white);
				iconLabel.setIcon(imgPet3);
				c = new GridBagConstraints();
				c.gridx=2;
				c.gridy=0;
				add(iconLabel,c);
				c.gridx=2;
				c.gridy=1;
				add(label,c);
				c.gridx=2;
				c.gridy=2;
				add(nameField,c);
				c.gridx=2;
				c.gridy=3;
				add(klar,c);
				c.gridx=2;
				c.gridy=4;
				add(load,c);
				c.gridx=2;
				c.gridy=5;
				add(exit,c);
				//c.gridx=0;
				//c.gridy=0;
				//c.gridwidth=1;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff1,c);
				//c.gridx=1;
				//c.gridy=0;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff2,c);
				//c.gridx=2;
				//c.gridy=0;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff3,c);
				//c.gridx=3;
				//c.gridy=0;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff4,c);
				revalidate();
				repaint();
			}else if(e.getSource().equals(pet4)){
				chosen=new EvilBunny("Kanin",0);
				remove(choice);
				remove(pet1);
				remove(pet2);
				remove(pet3);
				remove(pet4);
				nameField.setBackground(Color.white);
				label.setForeground(Color.white);
				iconLabel.setIcon(imgPet4);
				c = new GridBagConstraints();
				c.gridx=2;
				c.gridy=0;
				add(iconLabel,c);
				c.gridx=2;
				c.gridy=1;
				add(label,c);
				c.gridx=2;
				c.gridy=2;
				add(nameField,c);
				c.gridx=2;
				c.gridy=3;
				add(klar,c);
				c.gridx=2;
				c.gridy=4;
				add(load,c);
				c.gridx=2;
				c.gridy=5;
				add(exit,c);
				//c.gridx=0;
				//c.gridy=0;
				//c.gridwidth=1;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff1,c);
				//c.gridx=1;
				//c.gridy=0;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff2,c);
				//c.gridx=2;
				//c.gridy=0;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff3,c);
				//c.gridx=3;
				//c.gridy=0;
				//c.fill = GridBagConstraints.HORIZONTAL;
				//add(diff4,c);
				revalidate();
				repaint();
			}else if(e.getSource().equals(load)){
				final Player player = Profile.load("newsave.xml");
		    	final int rows = 27;
		    	final int cols = 50;
		    	final Map map = new Map(rows, cols, player);
				frame.swapToNewWorldMap(player, map);
			}else if(e.getSource().equals(klar)){
				
				Random generator= new Random();
				final Player player=new Player(nameField.getText(),100,generator.nextInt(90000),chosen, null, Difficulty.Normal,0);
				player.inventory.addItem(ItemKind.Lakan, 5);
		    	final int rows = 27;
		    	final int cols = 50;
		    	final Map map = new Map(rows, cols, player);
				frame.swapToNewWorldMap(player, map);
			
			}else if(e.getSource().equals(diff1)){
				chosenDiff=Difficulty.Easy;
				remove(diff1);
				remove(diff2);
				remove(diff3);
				remove(diff4);
				nameField.setBackground(Color.white);
				label.setForeground(Color.white);
				c.gridx=1;
				c.gridy=1;
				c.gridwidth=2;
				add(label,c);
				c.gridx=1;
				c.gridy=2;
				c.gridwidth=2;
				add(nameField,c);
				c.gridx=1;
				c.gridy=3;
				c.gridwidth=2;
				add(klar,c);
				revalidate();
				repaint();
			}else if(e.getSource().equals(diff2)){
				chosenDiff=Difficulty.Normal;
				remove(diff1);
				remove(diff2);
				remove(diff3);
				remove(diff4);
				nameField.setBackground(Color.white);
				label.setForeground(Color.white);
				c.gridx=1;
				c.gridy=1;
				c.gridwidth=2;
				add(label,c);
				c.gridx=1;
				c.gridy=2;
				c.gridwidth=2;
				add(nameField,c);
				c.gridx=1;
				c.gridy=3;
				c.gridwidth=2;
				add(klar,c);
				revalidate();
				repaint();
			}else if(e.getSource().equals(diff3)){
				chosenDiff=Difficulty.Difficult;
				remove(diff1);
				remove(diff2);
				remove(diff3);
				remove(diff4);
				nameField.setBackground(Color.white);
				label.setForeground(Color.white);
				c.gridx=1;
				c.gridy=1;
				c.gridwidth=2;
				add(label,c);
				c.gridx=1;
				c.gridy=2;
				c.gridwidth=2;
				add(nameField,c);
				c.gridx=1;
				c.gridy=3;
				c.gridwidth=2;
				add(klar,c);
				revalidate();
				repaint();
			}else if(e.getSource().equals(diff4)){
				chosenDiff=Difficulty.Hardcore;
				remove(diff1);
				remove(diff2);
				remove(diff3);
				remove(diff4);
				nameField.setBackground(Color.white);
				label.setForeground(Color.white);
				c.gridx=1;
				c.gridy=1;
				c.gridwidth=2;
				add(label,c);
				c.gridx=1;
				c.gridy=2;
				c.gridwidth=2;
				add(nameField,c);
				c.gridx=1;
				c.gridy=3;
				c.gridwidth=2;
				add(klar,c);
				revalidate();
				repaint();
			}else if(e.getSource().equals(exit)){
				System.exit(0);
			}
		}
	
	}
	
	private class BorderListener implements MouseListener{
		
		CustomButton button;
		CustomPetButton petButton;
		
		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			if(e.getComponent().getFont().equals(font)){
				button = (CustomButton) e.getComponent();
				button.setBorderPainted(true);
			}
			else if(e.getComponent().getFont().equals(font2)){
				petButton = (CustomPetButton) e.getComponent();
				petButton.setBorderPainted(true);
			}
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			if(e.getComponent().getFont().equals(font)){
				button = (CustomButton) e.getComponent();
				button.setBorderPainted(false);
			}
			else if(e.getComponent().getFont().equals(font2)){
				petButton = (CustomPetButton) e.getComponent();
				petButton.setBorderPainted(false);
			}
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}


}

